<?php
class ExpertAction extends UserAction{
	public $token;
	public $expert_model;
	public $expert_level_model;
	public $expert_title_model;
	public $consultation_model;
	public $solution_model;
	public $payment_model;
	public $userinfo_model;
	public function _initialize() {
		parent::_initialize();
		
		$this->expert_model=M('Experts');
		$this->expert_level_model=M('Experts_level');
		$this->expert_title_model=M('Experts_title');
		$this->consultation_model=M('Consultation');
		$this->solution_model = M('Experts_solution');
		$this->payment_model = M('Consult_payment');
		$this->userinfo_model = M('Userinfo');
		//
		$token_open=M('token_open')->field('queryname')->where(array('token'=>session('token')))->find();
	}
	public function index(){		
		$levelid=intval($_GET['level_id']);
		$levelid=$levelid==''?0:$levelid;
		
		$where=array('token'=>session('token'));
		if ($levelid){
			$where['level_id']=$levelid;
		}
        if(IS_POST){
            $key = $this->_post('searchkey');
            if(empty($key)){
                $this->error("关键词不能为空");
            }

            $map['token'] = $this->get('token'); 
            $map['expert_name|personal_profile|technical_desc|keywords'] = array('like',"%$key%"); 
            $list = $this->expert_model->where($map)->select(); 
            $count      = $this->expert_model->where($map)->count();       
            $Page       = new Page($count,20);
        	$show       = $Page->show();
        }else{
        	$count      = $this->expert_model->where($where)->count();
        	$Page       = new Page($count,20);
        	$show       = $Page->show();
        	$list = $this->expert_model->where($where)->order('expert_id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        }
    
		$this->assign('page',$show);		
		$this->assign('list',$list);
		$this->assign('isProductPage',1);
		$this->display();		
	}
	
	/**
	 * 专家级别管理
	 */
	public function levels(){		
		$where=array('token'=>session('token'));
        if(IS_POST){
            $key = $this->_post('searchkey');
            if(empty($key)){
                $this->error("关键词不能为空");
            }

            $map['token'] = $this->get('token'); 
            $map['name'] = array('like',"%$key%"); 
            $list = $this->expert_level_model->where($map)->select(); 
            $count      = $this->expert_level_model->where($map)->count();       
            $Page       = new Page($count,20);
        	$show       = $Page->show();
        }else{
        	$count      = $this->expert_level_model->where($where)->count();
        	$Page       = new Page($count,20);
        	$show       = $Page->show();
        	$list = $this->expert_level_model->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();
        }
		$this->assign('page',$show);		
		$this->assign('list',$list);
		
		$this->display();		
	}
	
	/**
	 * 添加专家级别
	 */
	public function levelAdd(){ 
		if(IS_POST){
			$this->insert('Experts_level','/levels');
		}else{
			$this->display('levelSet');
		}
	}
	
	/**
	 * 删除专家级别
	 */
	public function levelDel(){
		if($this->_get('token')!=session('token')){$this->error('非法操作');}
        $id = $this->_get('level_id');
        if(IS_GET){                              
            $where=array('level_id'=>$id,'token'=>session('token'));
            $check=$this->expert_level_model->where($where)->find();
            if($check==false)   $this->error('非法操作');
            $productsOfCat=$this->expert_model->where(array('catid'=>$id))->select;
            if (count($productsOfCat)){
            	$this->error('本专家级别下有专家，请删除专家后再删除此级别',U('Expert/levels',array('token'=>session('token'))));
            }
            $back=$this->expert_level_model->where($where)->delete();
            if($back==true){
            		$this->success('操作成功',U('Expert/levels',array('token'=>session('token'))));
            }else{
                 $this->error('服务器繁忙,请稍后再试',U('Expert/levels',array('token'=>session('token'))));
            }
        }        
	}
	
	/**
	 * 修改专家级别
	 */
	public function levelSet(){
        $id = $this->_get('level_id'); 
		$checkdata = $this->expert_level_model->where(array('level_id'=>$id))->find();
		if(empty($checkdata)){
            $this->error("没有相应记录.您现在可以添加.",U('Expert/levelAdd'));
        }
		if(IS_POST){ 
            $data=D('Experts_level');
            $where=array('level_id'=>$this->_post('level_id'),'token'=>session('token'));
			$check=$data->where($where)->find();
			if($check==false)$this->error('非法操作');
			if($data->create()){
				if($data->where($where)->save($_POST)){
						$this->success('修改成功',U('Expert/levels',array('token'=>session('token'))));
				}else{
					$this->error('操作失败');
				}
			}else{
				$this->error($data->getError());
			}
		}else{ 
			$this->assign('set',$checkdata);
			$this->display();	
		
		}
	}
	
	/**
	 * 职称管理
	 */
	public function titles(){
	    $where=array('token'=>session('token'));
	    if(IS_POST){
	        $key = $this->_post('searchkey');
	        if(empty($key)){
	            $this->error("关键词不能为空");
	        }
	
	        $map['token'] = $this->get('token');
	        $map['name'] = array('like',"%$key%");
	        $list = $this->expert_title_model->where($map)->select();
	        $count      = $this->expert_title_model->where($map)->count();
	        $Page       = new Page($count,20);
	        $show       = $Page->show();
	    }else{
	        $count      = $this->expert_title_model->where($where)->count();
	        $Page       = new Page($count,20);
	        $show       = $Page->show();
	        $list = $this->expert_title_model->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();
	    }
	    $this->assign('page',$show);
	    $this->assign('list',$list);
	
	    $this->display();
	}
	
	/**
	 * 添加职称
	 */
	public function titleAdd(){
	    if(IS_POST){
	        $this->insert('Experts_title','/titles');
	    }else{
	        $this->display('titleSet');
	    }
	}
	
	/**
	 * 删除职称
	 */
	public function titleDel(){
	    if($this->_get('token')!=session('token')){$this->error('非法操作');}
	    $id = $this->_get('title_id');
	    if(IS_GET){
	        $where=array('title_id'=>$id,'token'=>session('token'));
	        $check=$this->expert_title_model->where($where)->find();
	        if($check==false)   $this->error('非法操作');
	        $productsOfCat=$this->expert_model->where(array('title_id'=>$id))->select;
	        if (count($productsOfCat)){
	            $this->error('本职称下有专家，请删除专家后后再删除职称',U('Expert/titles',array('token'=>session('token'))));
	        }
	        $back=$this->expert_title_model->where($where)->delete();
	        if($back==true){
	            $this->success('操作成功',U('Expert/titles',array('token'=>session('token'))));
	        }else{
	            $this->error('服务器繁忙,请稍后再试',U('Expert/titles',array('token'=>session('token'))));
	        }
	    }
	}
	
	/**
	 * 修改职称
	 */
	public function titleSet(){
	    $id = $this->_get('level_id');
	    $checkdata = $this->expert_title_model->where(array('level_id'=>$id))->find();
	    if(empty($checkdata)){
	        $this->error("没有相应记录.您现在可以添加.",U('Expert/titleAdd'));
	    }
	    if(IS_POST){
	        $data=D('Experts_title');
	        $where=array('title_id'=>$this->_post('title_id'),'token'=>session('token'));
	        $check=$data->where($where)->find();
	        if($check==false)$this->error('非法操作');
	        if($data->create()){
	            if($data->where($where)->save($_POST)){
	                $this->success('修改成功',U('Expert/titles',array('token'=>session('token'))));
	            }else{
	                $this->error('操作失败');
	            }
	        }else{
	            $this->error($data->getError());
	        }
	    }else{
	        $this->assign('set',$checkdata);
	        $this->display();
	
	    }
	}
	
	public function add(){ 
		if(IS_POST){
			$this->all_insert('Experts','/index?token='.session('token'));
		}else{
			//分类
			$levelWhere=array('token'=>session('token'));
			$levels=$this->expert_level_model->where($levelWhere)->select();
			if (!$levels){
				 $this->error("请先添加专家级别",U('Expert/levelAdd',array('token'=>session('token'))));
				 exit();
			}
			
			//分类
			$titleWhere=array('token'=>session('token'));
			$titles=$this->expert_title_model->where($titleWhere)->select();
			if (!$titles){
			    $this->error("请先添加职称",U('Expert/titleAdd',array('token'=>session('token'))));
			    exit();
			}
			
			$wxuserWhere=array('token'=>session('token'));
			$wxusers=$this->userinfo_model->where($wxuserWhere)->select();
			
			// **添加校验，已经选择的微信用户不能在选择

			$this->assign('levels',$levels);
			$this->assign('titles',$titles);
			$levelsOptions=$this->levelsOptions($levels,0);
			$this->assign('levelsOptions',$levelsOptions);
			
			$titlesOptions=$this->titlesOptions($titles,0);
			$this->assign('titlesOptions',$titlesOptions);
			
			$wxusersOptions=$this->wxusersOptions($wxusers,0);
			$this->assign('wxusersOptions',$wxusersOptions);
			
			$this->assign('isProductPage',1);
			$this->display('set');
		}
	}
	/**
	 * 商品类别ajax select
	 *
	 */
	public function ajaxCatOptions(){
		$parentid=intval($_GET['parentid']);
		$data=M('Product_cat');
		$catWhere=array('parentid'=>$parentid,'token'=>session('token'));
		$cats=$data->where($catWhere)->select();
		$str='';
		if ($cats){
			foreach ($cats as $c){
				$str.='<option value="'.$c['id'].'">'.$c['name'].'</option>';
			}
		}
		$this->show($str);
	}
	public function set(){
        $expert_id = $this->_get('expert_id'); 
		$checkdata = $this->expert_model->where(array('expert_id'=>$expert_id))->find();
		if(empty($checkdata)){
            $this->error("没有相应记录.您现在可以添加.",U('Expert/add'));
        }
		if(IS_POST){ 
            $where=array('expert_id'=>$expert_id,'token'=>session('token'));
			$check=$this->expert_model->where($where)->find();
			if($check==false)$this->error('非法操作');
			if($this->expert_model->create()){
				if($this->expert_model->where($where)->save($_POST)){
					$this->success('修改成功',U('Expert/index',array('token'=>session('token'))));
					//$keyword_model=M('Keyword');
					//$keyword_model->where(array('token'=>session('token'),'pid'=>$this->_post('id'),'module'=>'Expert'))->save(array('keyword'=>$this->_post('keyword')));
				}else{
					$this->error('操作失败');
				}
			}else{
				$this->error($this->expert_model->getError());
			}
		}else{
			//专家级别分类
			$levelWhere=array('token'=>session('token'));
			$levels=$this->expert_level_model->where($levelWhere)->select();
			$this->assign('levels',$levels);
			
			$thisLevel=$this->expert_level_model->where(array('level_id'=>$checkdata['level_id']))->find();
			$this->assign('thisLevel',$thisLevel);
			
			//专家职称
			$titleWhere=array('token'=>session('token'));
			$titles=$this->expert_title_model->where($titleWhere)->select();
			$this->assign('titles',$titles);
				
			$thisTitle=$this->expert_title_model->where(array('title_id'=>$checkdata['title_id']))->find();
			$this->assign('thisTitle',$thisTitle);
			
			
			//专家职称
			$wxuserWhere=array('token'=>session('token'));
			$wxusers=$this->userinfo_model->where($wxuserWhere)->select();
			$this->assign('wxusers',$wxusers);
			
			$thisWxuser = $this->userinfo_model->where(array('id'=>$checkdata['userinfo_id']))->find();
			$this->assign('thisWxuser',$thisWxuser);
			
			$this->assign('isUpdate',1);
			$levelsOptions=$this->levelsOptions($levels,$checkdata['level_id']);
			$this->assign('levelsOptions',$levelsOptions);
			
			$titlesOptions=$this->titlesOptions($titles,$checkdata['title_id']);
			$this->assign('titlesOptions',$titlesOptions);
			
			$wxusersOptions=$this->wxusersOptions($wxusers,$checkdata['userinfo_id']);
			$this->assign('wxusersOptions',$wxusersOptions);
			
			 
			//
			$this->assign('set',$checkdata);
			$this->assign('isProductPage',1);
			if($_GET['type'] == 'info')
			{
			    
			    $this->display('info');
			}
			else
			{
			    $this->display();
			}
			
		
		}
	}
	
	//专家级别下拉列表
	public function levelsOptions($levels,$selectedid){
	    $str='';
	    if ($levels){
	        foreach ($levels as $l){
	            $selected='';
	            if ($l['level_id']==$selectedid){
	                $selected=' selected';
	            }
	            $str.='<option value="'.$l['level_id'].'"'.$selected.'>'.$l['name'].'</option>';
	        }
	    }
	    return $str;
	}
	
	//商品类别下拉列表
	public function titlesOptions($titles,$selectedid){
	    $str='';
	    if ($titles){
	        foreach ($titles as $t){
	            $selected='';
	            if ($t['title_id']==$selectedid){
	                $selected=' selected';
	            }
	            $str.='<option value="'.$t['title_id'].'"'.$selected.'>'.$t['name'].'</option>';
	        }
	    }
	    return $str;
	}
	
	public function wxusersOptions($wxusers,$selectedid)
	{
	    $str='';
	    if ($wxusers){
	        foreach ($wxusers as $u){
	            $selected='';
	            if ($u['title_id']==$selectedid){
	                $selected=' selected';
	            }
	            $str.='<option value="'.$u['id'].'"'.$selected.'>'.$u['wechaname'].'</option>';
	        }
	    }
	    return $str;
	}
	
	public function del(){
		if($this->_get('token')!=session('token')){$this->error('非法操作');}
        $id = $this->_get('expert_id');
        if(IS_GET){                              
            $where=array('expert_id'=>$id,'token'=>session('token'));
            $check=$this->expert_model->where($where)->find();
            if($check==false)   $this->error('非法操作');

            $back=$this->expert_model->where($where)->delete();
            if($back==true){
            	//$keyword_model=M('Keyword');
            	//$keyword_model->where(array('token'=>session('token'),'pid'=>$id,'module'=>'Product'))->delete();
                $this->success('操作成功',U('Expert/index',array('token'=>session('token'))));
            }else{
                 $this->error('服务器繁忙,请稍后再试',U('Expert/index',array('token'=>session('token'))));
            }
        }        
	}
	
	public function authStatus()
	{
	    if($this->_get('token')!=session('token')){$this->error('非法操作');}
	    $id = $this->_get('expert_id');
	    if(IS_GET){
	        $where=array('expert_id'=>$id,'token'=>session('token'));
	        $check=$this->expert_model->where($where)->find();
	        if($check==false)   $this->error('非法操作');
	    
	        if($check['auth_status'] == 1)
	        {
	            $data['auth_status'] = 0;
	            $check2 = $this->expert_model->where($where)->save($data);
	            if($check2)
	            {
	                $this->success('您已成功取消专家【'.$check['expert_name'].'】的认证！',U('Expert/index',array('token'=>session('token'))));
	            }
	        }
	        else
	        {
	            $data["auth_status"] = 1;
	            $check2 = $this->expert_model->where($where)->save($data);
	            if($check2)
	            {
	                $this->success('专家【'.$check['expert_name'].'】认证成功！',U('Expert/index',array('token'=>session('token'))));
	            }
	        }
	    }
	}
	
	public function consultations(){
	    $where=array('token'=>session('token'));
	    if(IS_POST){
	        $key = $this->_post('searchkey');
	        if(empty($key)){
	            $this->error("关键词不能为空");
	        }
	    
	        $map['token'] = $this->get('token');
	        $map['consult_desc|addto_desc'] = array('like',"%$key%");
	        $list = $this->consultation_model->where($map)->select();
	        $count      = $this->consultation_model->where($map)->count();
	        $Page       = new Page($count,20);
	        $show       = $Page->show();
	    }else{
	        $count      = $this->consultation_model->where($where)->count();
	        $Page       = new Page($count,20);
	        $show       = $Page->show();
	        $list = $this->consultation_model->where($where)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
	    }
	    
	    foreach($list as $ks=>$vs)
		{	 
		    $userinfo  = M('Userinfo')->where(array('id'=>$list[$ks]['userinfo_id']))->find();
		    $list[$ks]['username'] = $userinfo['wechaname'];
			if($list[$ks]['consult_type'] == 0)
			{
				$list[$ks]['consult_type'] = '无偿';
			}	
			elseif ($list[$ks]['consult_type'] == 1)
			{
			    $list[$ks]['consult_type'] = '指定';
			}	
			else 
			{
			    $list[$ks]['consult_type'] = '竞标';
			}
			$list[$ks]['strStatus'] = $this->consultationStatus($list[$ks]['status']);;
        }
	    
	    $this->assign('page',$show);
	    $this->assign('list',$list);
	    $this->assign('isProductPage',1);
	    $this->display();
	}
	
	public function refund()
	{
	    $consultation=$this->consultation_model->where(array('id'=>intval($_GET['id'])))->find();
	    if($consultation)
	    {
	        $this->consultation_model->where(array('id'=>intval($_GET['id'])))->save(array('status'=>12));// 将咨询状态修改为已退款
	        $this->success('已退款！',U('Expert/consultations',array('token'=>session('token'))));
	    }
	}
	
	public function consultationInfo(){
		$consultation=$this->consultation_model->where(array('id'=>intval($_GET['id'])))->find();
		//检查权限
		if (strtolower($consultation['token'])!=strtolower($this->_session('token'))){
			exit();
		}
		
		$userinfo  = M('Userinfo')->where(array('id'=>$consultation['userinfo_id']))->find();
		$consultation['username'] = $userinfo['wechaname'];
		
		if($consultation['consult_type'] == 0)
		{
		      $consultation['consult_type'] = '无偿';
		}
		elseif ($consultation['consult_type'] == 1)
		{
		      $consultation['consult_type'] = '指定';
		}
		else
		{
		      $consultation['consult_type'] = '竞标';
		}
		$this->assign('set',$consultation);
		$this->display();
	}
	
	public function delConsultation()
	{
	    $consultation=$this->consultation_model->where(array('id'=>intval($_GET['id'])))->find();
	    if(!$consultation)
	    {
	        $this->error('非法操作');
	    }
	    $check = $this->consultation_model->where(array('id'=>intval($_GET['id'])))->save(array('is_delete'=>'1'));
	    if($check)
	    {
	        $this->success('您已强制取消该咨询！',U('Expert/consultations',array('token'=>session('token'))));
	    }
	}
	
	public function solutions(){
	    $where=array('consult_id'=>$_GET['id']);
	    if(IS_POST){
	        $key = $this->_post('searchkey');
	        if(empty($key)){
	            $this->error("关键词不能为空");
	        }
	         
	        $map['solution_desc|addsolution_desc'] = array('like',"%$key%");
	        $list = $this->solution_model->where($map)->select();
	        $count      = $this->solution_model->where($map)->count();
	        $Page       = new Page($count,20);
	        $show       = $Page->show();
	    }else{
	        $count      = $this->solution_model->where($where)->count();
	        $Page       = new Page($count,20);
	        $show       = $Page->show();
	        $list = $this->solution_model->where($where)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
	    }
	     
	    foreach($list as $ks=>$vs)
	    {
	        $expert  = $this->expert_model->where(array('expert_id'=>$list[$ks]['expert_id']))->find();
	        $list[$ks]['expert_name'] = $expert['expert_name'];
	    }
	     
	    $this->assign('page',$show);
	    $this->assign('list',$list);
	    $this->assign('isProductPage',1);
	    $this->display();
	}
	
	Public function confirmPayExpert()
	{
	    $where=array('id'=>$_GET['id']);
	    $payment = $this->payment_model->where($where)->save(array('pay_status'=>1,'money_paid'=>$_GET['money_paid'],'pay_time'=>time()));
	   // $this->success('您已成功支付给专家！',U('Expert/payment',array('id'=>$payment['consult_id'],'token'=>session('token'))));
	    $this->success('您已成功支付给专家！',U('Expert/index',array('token'=>session('token'))));
	}
	
	public function payment(){
	    $where=array('consult_id'=>$_GET['id']);
	   	$payments = $this->payment_model->where($where)->select();
	   	
	   	foreach ($payments as $ks=>$vs)
	   	{
	   	    $expert  = $this->expert_model->where(array('expert_id'=>$payments[$ks]['expert_id']))->find();
	   	    $payments[$ks]['expert_name'] = $expert['expert_name'];
	   	    
	   	    if($payments[$ks]['pay_status'] == 1)
	   	    {
	   	        $payments[$ks]['status'] = '已支付';
	   	    }
	   	    else
	   	    {
	   	        $payments[$ks]['status'] = '未支付';
	   	    }
	   	     
	   	    if($payments[$ks]['pay_type'] == 1)
	   	    {
	   	        $payments[$ks]['type'] = '微信支付';
	   	    }
	   	    elseif($payments[$ks]['pay_type'] == 2)
	   	    {
	   	        $payments[$ks]['type'] = '支付宝支付';
	   	    }
	   	    else
	   	    {
	   	        $payments[$ks]['type'] = '';
	   	    }
	   	    
	   	    
	   	}
	   	
	    $this->assign('payments',$payments);
	    $this->display();
	}
	
	/**
	 * 咨询状态转换  咨询状态：0 咨询问题保存；1 咨询待支付；2 咨询已支付；3 咨询已分发；4 咨询已抢答；5 咨询解答中；6 咨询追问；7 咨询结束；8 退款
	 * @param unknown $st
	 * @return string
	 */
	public function consultationStatus($ct)
	{
	    switch($ct) {
	        case 1:
	            $status = '咨询待支付';
	            break;
	        case 2:
	            $status = '咨询已支付';
	            break;
	        case 3:
	            $status = '咨询已分发';
	            break;
	        case 4:
	            $status = '咨询已抢答';
	            break;
	        case 5:
	            $status = '咨询解答中';
	            break;
	        case 6:
	            $status = '咨询追问';
	            break;
	        case 7:
                $status = '咨询结束(取消)';
                break;
            case 8:
                $status = '咨询结束(已解答)';
                break;
            case 9:
                $status = '咨询结束(无解答)';
                break;
            case 10:
                $status = '咨询结束(提前)';
                break;
            case 11:
                $status = '申请退款';
                break;
            case 12:
                    $status = '已退款';
                    break;
	        default:
	            $status = '咨询问题保存';
	            break;
	    }
	
	    return $status;
	}
	
	public function wxUsersMgr()
	{
	    $where=array('token'=>session('token'));
	    if(IS_POST){
	        $key = $this->_post('searchkey');
	        if(empty($key)){
	            $this->error("关键词不能为空");
	        }
	    
	        $map['token'] = $this->get('token');
	        $map['wechaname|truename|address|company'] = array('like',"%$key%");
	        $list = $this->userinfo_model->where($map)->select();
	        $count      = $this->userinfo_model->where($map)->count();
	        $Page       = new Page($count,20);
	        $show       = $Page->show();
	    }else{
	        $count      = $this->userinfo_model->where($where)->count();
	        $Page       = new Page($count,20);
	        $show       = $Page->show();
	        $list = $this->userinfo_model->where($where)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
	    }
	    
	    foreach($list as $ks=>$vs)
		{	 
			if($list[$ks]['sex'] == 1)
			{
				$list[$ks]['sex'] = '女';
			}	
			else 
			{
			    $list[$ks]['sex'] = '男';
			}
        }
	    
	    $this->assign('page',$show);
	    $this->assign('list',$list);
	    $this->assign('isProductPage',1);
	    $this->display();
	}
}


?>